package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootZnx;
import org.footballmanager.pandafootball.system.form.impl.FootZnxSearchForm;
import org.footballmanager.pandafootball.system.mapper.FootZnxMapper;
import org.footballmanager.pandafootball.system.service.FootZnxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Slf4j
@Service("footZnxService")
public class FootZnxServiceImpl extends ServiceImpl<FootZnx> implements FootZnxService {
    @Autowired
    private FootZnxMapper footZnxMapper;

	@Override
	public void sendZnx(Long sender, Long reciver, String content, Long logo, String url, HttpServletRequest request){
		FootZnx znx = new FootZnx();
		znx.setSender(sender);
		znx.setResiver(reciver);
		znx.setSendTime(new Date());
		znx.setReaded(false);
		znx.setContent(content);
		znx.setLogo(logo);
		znx.setUrl(url);
		this.save(znx, request);
	}

	@Override
	public List<FootZnx> findZnxsByUserId(Long userId) {
		Example example = new Example(FootZnx.class);
		Example.Criteria criteria = example.createCriteria();
		Example.Criteria criteria2 = example.createCriteria();
		criteria.andEqualTo("sender", userId);
		criteria2.andEqualTo("resiver", userId);
		return this.findall(example);
	}

	@Override
	public void readAll(Long userId, HttpServletRequest request) {
		List<FootZnx> list = this.findZnxsByUserId(userId);
		if(list != null && !list.isEmpty()){
			for(FootZnx znx: list){
				znx.setReaded(true);
				this.updateNotNull(znx, request);
			}
		}
	}

    /**
     * 翻页方法
     *
     * @param footZnxSearchForm 条件
     * @return 列表
     */
    @Override
    public List<FootZnx> findall(FootZnxSearchForm footZnxSearchForm) {
        return footZnxMapper.findall(footZnxSearchForm);
    }

	/**
	 * 根据主键获取记录
	 * @param id 主键
	 * @return 记录
	 */
	@Override
	public FootZnx get(Serializable id) {
		return footZnxMapper.findById(id);
	}

    /**
     * 读取站内信
     *
     * @param id      站内信主键
     * @param request 请求
     * @return 成功或失败
     */
    @Override
    public String znxyd(Long id, HttpServletRequest request) {
        FootZnx znx = this.get(id);
        if(znx != null){
            znx.setReaded(true);
            this.updateNotNull(znx, request);
            return Constants.OK;
        }
        return Constants.ERROR;
    }

    /**
     * 查询
     *
     * @param entityname        实体类名
     * @param map               参数
     * @param footZnxSearchForm 条件
     */
    @Override
    public void search(String entityname, Model map, FootZnxSearchForm footZnxSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footZnxSearchForm);
        Page<FootZnx> page = PageHelper.startPage(footZnxSearchForm.getPageIndex(), footZnxSearchForm.getPageSize());
        List<FootZnx> list = this.findall(footZnxSearchForm);
        map.addAttribute(Constants.PI, page.toPageInfo());
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
    }

    /**
     * 删除所有
     *
     * @param footZnxSearchForm 条件
     * @param request           请求
     */
    @Override
    public void deleteAll(FootZnxSearchForm footZnxSearchForm, HttpServletRequest request) {
        if(footZnxSearchForm.getSelectIds() != null){
            String[] result = footZnxSearchForm.getSelectIds().split(Constants.COMMA);
            for(String id: result){
                this .delete(Long.parseLong(id), request);
            }
        }
    }

    /**
     * 设置站内信状态为已读
     *
     * @param id 站内信主键
     * @param request 请求
     * @return 站内信实体
     */
    @Override
    public FootZnx read(Long id, HttpServletRequest request) {
        FootZnx znx = this.get(id);
        if(znx != null){
            znx.setReaded(true);
            this.updateNotNull(znx, request);
        }
        return znx;
    }
}
